package com.test.platform.module.perf.controller;

import com.test.common.form.perf.TaskCreateForm;
import com.test.common.form.perf.TaskExecFrom;
import com.test.common.form.perf.TaskScriptForm;
import com.test.common.form.perf.TaskSearchForm;
import com.test.common.response.ResponseResult;
import com.test.platform.module.perf.service.PerfTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

@Slf4j
@Api(value = "value", tags = "性能平台任务管理")
@RestController
@RequestMapping("/perf/task")
@ResponseResult
public class PerfTaskController {

    @Resource
    private PerfTaskService perfTaskService;

    @ApiOperation("查询测试任务信息")
    @PostMapping("/search")
    public Object search(@RequestBody TaskSearchForm taskSearchForm) {
        log.info("查询测试任务信息: {}", taskSearchForm);
        return perfTaskService.search(taskSearchForm);
    }

    @ApiOperation("创建性能任务")
    @PostMapping("/create")
    public Object create(@RequestBody TaskCreateForm taskCreateForm) {
        log.info("创建性能任务|{}", taskCreateForm);
        return perfTaskService.create(taskCreateForm);
    }

    @ApiOperation("修改性能任务")
    @PostMapping("/modify")
    public Object modify(@RequestBody TaskCreateForm taskCreateForm) {
        log.info("修改性能任务|{}", taskCreateForm);
        return perfTaskService.modify(taskCreateForm);
    }

    @ApiOperation("查询任务下的所有脚本")
    @GetMapping("/getScripts")
    public Object getScripts(int taskId) {
        return perfTaskService.getScripts(taskId);
    }

    @ApiOperation("任务添加关联脚本")
    @PostMapping("/add/scripts")
    public Object addScripts(@RequestBody ArrayList<TaskScriptForm> taskScriptFormList) {
        log.info("任务添加关联脚本|{}", taskScriptFormList);
        return perfTaskService.addScripts(taskScriptFormList);
    }

    @ApiOperation("任务移除关联脚本")
    @GetMapping("/remove/scripts")
    public Object removeScripts(int taskId, int[] scriptIds) {
        log.info("任务移除关联脚本|{}-{}", taskId, scriptIds);
        return perfTaskService.removeScripts(taskId, scriptIds);
    }

    @ApiOperation("执行任务脚本")
    @PostMapping("/exec/scripts")
    public Object execScript(@RequestBody TaskExecFrom taskExecFrom) {
        log.info("执行任务脚本|{}", taskExecFrom);
        return perfTaskService.execScript(taskExecFrom);
    }

    @ApiOperation("查询任务下的所有报告")
    @GetMapping("/find/reports")
    public Object findReports(@RequestParam int taskId, @RequestParam int page, @RequestParam int size) {
        log.info("查询任务下的所有报告|{}", taskId);
        return perfTaskService.findReports(taskId, page, size);
    }

    @ApiOperation("查询任务下脚本的所有报告")
    @GetMapping("/find/script/reports")
    public Object findScriptReports(@RequestParam int taskId, @RequestParam int scriptId, @RequestParam int page, @RequestParam int size) {
        log.info("查询任务下的所有报告|{}", taskId);
        return perfTaskService.findScriptReports(taskId, scriptId, page, size);
    }

    @GetMapping("/update/script/usersOrQps")
    public Object updateScriptUsersOrQps(int taskId, int scriptId, String key, int value) {
        log.info("更新任务中脚本的并发或QPS设定：{}={}", key, value);
        return perfTaskService.updateScriptUsersOrQps(taskId, scriptId, key, value);
    }
}
